package cn.sc.allinpay.allfavour.business.common.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.math.BigDecimal;

import cn.sc.allinpay.allfavour.R;

/**
 * description：金额输入弹出框
 * author：pany
 * on 2018/1/16 16:34
 */
public class InputAmtPopWindow extends PopWindow implements NumberKeyboardLayout.OnKeyListener{

    private OnInputAmtListener onInputAmtListener;
    private BigDecimal bai = new BigDecimal(100);
    private BigDecimal amount = BigDecimal.ZERO;
    private StringBuffer inputAmount = new StringBuffer();

    private NumberKeyboardLayout keybord;
    private TextView tv_input;
    private TextView tips;
    private TextView title;

    public InputAmtPopWindow(Activity activity) {
        super(activity);
    }

    @Override
    protected int layoutId() {
        return R.layout.pop_input_amt;
    }

    @Override
    protected void layoutView(View view) {
        tv_input = find(R.id.tv_input);
        tips = find(R.id.tips);
        title = find(R.id.title);

        keybord = find(R.id.keybord);
        keybord.setOnKeyListener(this);
    }

    @Override
    protected int getHeight() {
        int height = getScreenHeight(getActivity());
        return (int) (height*0.8f);
    }
    /**
     * 获取屏幕高度
     */
    public int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    @Override
    protected int getAnimationStyle() {
        return R.style.pop_window_anim;
    }

    public void show(View view,String titleText) {
        title.setText(titleText);
        onKey(null,11);
        show(view);
    }

    @Override
    public void onKey(View view, int index) {
        switch (index) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 10:
                tips.setTextColor(Color.rgb(51, 51, 51));
                tips.setText("输入金额（￥）");
                if (inputAmount.length() < 9) {
                    inputAmount.append(((Button) view).getText().toString());
                }
                break;
            case 12://删除
                tips.setTextColor(Color.rgb(51, 51, 51));
                tips.setText("输入金额（￥）");
                if (inputAmount.length() > 0) {
                    inputAmount.deleteCharAt(inputAmount.length() - 1);
                }
                break;
            case 11://清空
                tips.setTextColor(Color.rgb(51, 51, 51));
                tips.setText("输入金额（￥）");
                inputAmount.setLength(0);
                break;
            case 9:///取消
                dismiss();
                break;
            case 13://确定
                if (inputAmount.length() > 10) {
                    tips.setTextColor(Color.rgb(255, 0, 0));
                    tips.setText("输入位数有误,最大不超过10位");
                } else {
                    if (amount.compareTo(BigDecimal.ZERO) < 1) {
                        tips.setTextColor(Color.rgb(255, 0, 0));
                        tips.setText("消费金额不能为0");
                    } else {
                        String amt = tv_input.getText().toString().trim();
                        dismiss();
                        if(onInputAmtListener!=null) {
                            onInputAmtListener.onInputAmt(amt);
                        }
                    }
                }
                break;
        }
        if (inputAmount.length() > 0) {
            amount = new BigDecimal(inputAmount.toString());
            amount = amount.multiply(new BigDecimal("1.00"));
            String strShow = amount.divide(bai).toString();
            tv_input.setText(strShow);
        } else {
            amount = BigDecimal.ZERO;
            tv_input.setText("0.00");
        }
    }

    public void setOnInputAmtListener(OnInputAmtListener onInputAmtListener) {
        this.onInputAmtListener = onInputAmtListener;
    }

    public interface OnInputAmtListener {
        void onInputAmt(String inputAmt);
    }
}
